package cn.wolfcode.luowowo.article.service.impl;

import cn.wolfcode.luowowo.article.domain.Destination;
import cn.wolfcode.luowowo.article.domain.Region;
import cn.wolfcode.luowowo.article.mapper.DestinationMapper;
import cn.wolfcode.luowowo.article.query.DestinationQuery;
import cn.wolfcode.luowowo.article.service.IDestinationService;
import cn.wolfcode.luowowo.article.service.IRegionService;
import com.alibaba.dubbo.config.annotation.Reference;
import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

@Service
public class DestinationServiceImpl implements IDestinationService {
    @Autowired
    private DestinationMapper destinationMapper;
    @Reference
    private IRegionService regionService;

    @Override
    public List<Destination> getDestByDeep(int deep) {
        return destinationMapper.getDestByDeep(deep);
    }

    @Override
    public List<Destination> getDestByRegionId(Long rid) {
        //判断rid是否为-1,因为rid为-1时表示查询国内所有省份
        if (rid == -1){
            //如果rid是-1,那么直接查国内的所有省份
            return destinationMapper.selectSubDestByParentId(1L);
        }

        //先查出该区域id相对应的区域
       Region region = regionService.get(rid);
        //再根据该区域查出它相关联的目的地id
        Long[] ids = region.getRefIds();
        return destinationMapper.getDestByIds(ids);
    }

    @Override
    public PageInfo query(DestinationQuery qo) {
        PageHelper.startPage(qo.getCurrentPage(),qo.getPageSize());
        return new PageInfo(destinationMapper.selectForList(qo));
    }

    @Override
    public List<Destination> getToasts(Long parentId) {
        List<Destination> list = new ArrayList<>();
        creatToast(parentId,list);
        Collections.reverse(list);//列表倒序
        return list;
    }

    @Override
    public void changeHotValue(boolean hot, Long id) {
        destinationMapper.changeHotValue(hot,id);
    }

    private void creatToast(Long parentId, List<Destination> list) {
        //找到父id的目的地
        Destination destination = destinationMapper.selectByPrimaryKey(parentId);
        //如果获取的目的地为空,则表示已到最大的id
        if (destination==null){
            return;
        }
        list.add(destination);
        //如果不为null,则把该目的地的id加入吐司中
        if (destination.getParent()!=null){
            creatToast(destination.getParent().getId(),list);
        }
    }

    @Override
    public Destination getCountry(Long id){
        List<Destination> list = this.getToasts(id);
        if (list != null && list.size() > 0){
            return list.get(0);
        }
        return null;
    }

    @Override
    public Destination getProvince(Long id) {
        //必须是国内的才设置省份
        List<Destination> list = this.getToasts(id);
        if (list != null && list.size() > 0){
            //国外
            if (list.get(0).getId() != 1){
                return null;
            }
        }
        if (list.size() > 1){
            return list.get(1);
        }
        return null;
    }

    @Override
    public List<Destination> list() {
        return destinationMapper.selectAll();
    }
}
